//
//  MinePromptViewController.m
//  YongChuanTradingArea
//
//  Created by Vivian on 2017/9/26.
//  Copyright © 2017年 李冰. All rights reserved.
//

#import "MinePromptViewController.h"
// 弹出分享菜单需要导入的头文件
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
// 自定义分享菜单栏需要导入的头文件
#import <ShareSDKUI/SSUIShareActionSheetStyle.h>
#import <ShareSDKUI/SSUIShareActionSheetItem.h>
#import <ShareSDKUI/SSUIShareActionSheetCustomItem.h>

#import <ShareSDKUI/SSUIEditorViewStyle.h>

@interface MinePromptViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageWidth;
@property (weak, nonatomic) IBOutlet UIView *screenshotView;
@property (nonatomic, copy) NSString * imgUrl;
@end

@implementation MinePromptViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"推广码";
    
    for (UserModelImgList *model in [LBUserManage defaultManager].userModel.ImgList) {
        if ([model.imgtype isEqualToString:@"QRCODE"]) {
            [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:PLACE_HOLDER];
            self.imgUrl = model.image;
        }
    }
    self.heightCons.constant = AdaptationRatioH(345.5);
    self.imageWidth.constant = AdaptationRatioW(150);
    // Do any additional setup after loading the view from its nib.
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    
    return UIStatusBarStyleDefault;
}

- (IBAction)shareBtnAction:(UIButton *)sender {
    if (self.imgUrl.length == 0)
    {
        [MBProgressHUD showMessageWithOutIcon:@"推广码获取失败"];
        return;
    }
    UIImage * img = [self screenShotView:self.screenshotView];
    WEAKSELF;
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:@"省点云购"
                                     images:img //传入要分享的图片
                                        url:nil
                                      title:@"省点云购"
                                       type:SSDKContentTypeAuto];
    
    SSUIShareActionSheetItem *item = [SSUIShareActionSheetItem itemWithIcon:[UIImage imageNamed:@"sns_icon_1.png"] label:@"新浪微博" onClick:^{
        
        [weakSelf _shareToPlatform:shareParams type:SSDKPlatformTypeSinaWeibo];
    }];
    SSUIShareActionSheetItem *item1 = [SSUIShareActionSheetItem itemWithIcon:[UIImage imageNamed:@"sns_icon_22.png"] label:@"微信好友" onClick:^{
        
        [weakSelf _shareToPlatform:shareParams type:SSDKPlatformSubTypeWechatSession];
    }];
    SSUIShareActionSheetItem *item2 = [SSUIShareActionSheetItem itemWithIcon:[UIImage imageNamed:@"sns_icon_23.png"] label:@"微信朋友圈" onClick:^{
        
        [weakSelf _shareToPlatform:shareParams type:SSDKPlatformSubTypeWechatTimeline];
    }];
    SSUIShareActionSheetItem *item3 = [SSUIShareActionSheetItem itemWithIcon:[UIImage imageNamed:@"sns_icon_24.png"] label:@"QQ" onClick:^{
        
        [weakSelf _shareToPlatform:shareParams type:SSDKPlatformSubTypeQQFriend];
    }];
    SSUIShareActionSheetItem *item4 = [SSUIShareActionSheetItem itemWithIcon:[UIImage imageNamed:@"sns_icon_21.png"] label:@"复制链接" onClick:^{
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [MBProgressHUD showMessageWithOutIcon:@"复制成功!"];
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = self.imgUrl;
        });
    }];
    // 设置分享菜单栏样式（非必要）
    // 设置分享菜单的背景颜色
    [SSUIShareActionSheetStyle setActionSheetBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]];
    // 设置分享菜单颜色
    [SSUIShareActionSheetStyle setActionSheetColor:[UIColor whiteColor]];
    // 设置分享菜单－取消按钮背景颜色
    [SSUIShareActionSheetStyle setCancelButtonBackgroundColor:[UIColor colorWithRed:21.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0]];
    // 设置分享菜单－取消按钮的文本颜色
    [SSUIShareActionSheetStyle setCancelButtonLabelColor:[UIColor blackColor]];
    // 设置分享菜单－社交平台文本颜色
    [SSUIShareActionSheetStyle setItemNameColor:[UIColor colorWithHex:0x999999]];
    // 设置分享菜单－社交平台文本字体
    [SSUIShareActionSheetStyle setItemNameFont:[UIFont systemFontOfSize:10]];
    //设置简单分享菜单样式
    [SSUIShareActionSheetStyle setShareActionSheetStyle:ShareActionSheetStyleSimple];
    //2、弹出ShareSDK分享菜单
    [ShareSDK showShareActionSheet:self.view
                             items:@[item,item1,item2,item3,item4]
                       shareParams:nil
               onShareStateChanged:nil];
}

- (void) _shareToPlatform:(NSMutableDictionary *) params type:(SSDKPlatformType) type {
    
    //进行分享
    [ShareSDK share:type //传入分享的平台类型
         parameters:params
     onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
     }];
}

//传入需要截取的view
-(UIImage *)screenShotView:(UIView *)view{
    UIImage *imageRet = [[UIImage alloc]init];
    UIGraphicsBeginImageContextWithOptions(view.frame.size, false, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    imageRet = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return imageRet;
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.translucent = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBar.translucent = NO;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
